package Swing;

import MsgSystem.ImpInstantMessage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.Desktop;
import java.net.*;
import javax.swing.JOptionPane;

public class Inicio extends javax.swing.JFrame {

    ImpInstantMessage itm = new ImpInstantMessage();
    String username, password;

    public Inicio() {
        initComponents();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Login = new javax.swing.JToggleButton();
        UsernameField = new javax.swing.JTextField();
        PasswordField = new javax.swing.JPasswordField();
        UsernameText = new javax.swing.JLabel();
        PasswordText = new javax.swing.JLabel();
        RegisterLink = new javax.swing.JLabel();
        version = new javax.swing.JLabel();
        PhotoWorldTitle = new javax.swing.JLabel();
        RememberPassword = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PhotoWorld Login");
        setResizable(false);

        Login.setText("Login");
        Login.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginActionPerformed(evt);
            }
        });

        UsernameField.setText("@gmail.com");

        UsernameText.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        UsernameText.setText("Username");

        PasswordText.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        PasswordText.setText("Password");

        RegisterLink.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        RegisterLink.setForeground(new java.awt.Color(0, 0, 255));
        RegisterLink.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        RegisterLink.setText("Register");
        RegisterLink.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        RegisterLink.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                RegisterRedirect(evt);
            }
        });

        version.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        version.setText("Version 1.0");

        PhotoWorldTitle.setFont(new java.awt.Font("Wide Latin", 3, 48)); // NOI18N
        PhotoWorldTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        PhotoWorldTitle.setText("PhotoWorld");

        RememberPassword.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        RememberPassword.setForeground(new java.awt.Color(0, 0, 255));
        RememberPassword.setText("Forgot your password? ");
        RememberPassword.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        RememberPassword.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LoginProblems(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(RegisterLink)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(version)
                .addGap(31, 31, 31))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(102, 102, 102)
                .addComponent(PhotoWorldTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
                .addGap(91, 91, 91))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(PasswordText)
                    .addComponent(UsernameText))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RememberPassword)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(UsernameField)
                        .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(Login))
                .addGap(271, 271, 271))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(PhotoWorldTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UsernameText)
                    .addComponent(UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PasswordText))
                .addGap(14, 14, 14)
                .addComponent(RememberPassword)
                .addGap(18, 18, 18)
                .addComponent(Login)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RegisterLink)
                    .addComponent(version))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginActionPerformed
    boolean connect=false;
    connect=itm.login(UsernameField.getText(), PasswordField.getText(), "Home");
    if(connect==true){
    this.itm = itm;
    Tabs a = new Tabs(itm);
    this.setVisible(false);
    a.setVisible(true);
    }
    else{
       JOptionPane.showMessageDialog(null, "Username or password incorrect. Try again!",
                            "Connection error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_LoginActionPerformed

    private void RegisterRedirect(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RegisterRedirect

        try {
            try {
                Desktop.getDesktop().browse(new URI("https://accounts.google.com/SignUp?service=mail&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default"));

            } catch (URISyntaxException ex) {
                Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
        }



    }//GEN-LAST:event_RegisterRedirect

    private void LoginProblems(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LoginProblems
        // TODO add your handling code here:
        try {
            try {
                Desktop.getDesktop().browse(new URI("https://www.google.com/accounts/recovery?gaps=AHwGkRkfKg_NCWnGae51Ej-g7mqmSAdKV4ZzBi-8_XzSyycrWZ3WVqdJtsPyyulTHfAoMf25VsFB0o1i5iIBnxHxHGOzxVIvMg&service=mail&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F"));
            } catch (URISyntaxException ex) {
                Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_LoginProblems

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Inicio().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton Login;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel PasswordText;
    private javax.swing.JLabel PhotoWorldTitle;
    private javax.swing.JLabel RegisterLink;
    private javax.swing.JLabel RememberPassword;
    private javax.swing.JTextField UsernameField;
    private javax.swing.JLabel UsernameText;
    private javax.swing.JLabel version;
    // End of variables declaration//GEN-END:variables
}
